package service

import (
	"fmt"
	"ginblog/model"
	errMsg "ginblog/model/err"
	"ginblog/model/request"
	"ginblog/model/response"
	"gorm.io/gorm"
)

func GetCategory(id int) (*model.Category, errMsg.Code) {
	var (
		data = model.Category{
			ID: uint(id),
		}
	)
	err := data.Get()
	r := NewReturn()
	r.Check(err != nil && err != gorm.ErrRecordNotFound, errMsg.DB_ERROR)
	r.Check(err == gorm.ErrRecordNotFound, errMsg.ERROR_CATEGORY_NOT_EXIST)
	return &data, r.C
}

func AddCategory(data *model.Category) errMsg.Code {
	n, err := data.Create()
	r := NewReturn()
	r.Check(err != nil, errMsg.DB_ERROR)
	r.Check(n < 1, errMsg.DB_OPTION_FAIL)
	return r.C
}
func DeleteCategory(data *model.Category) errMsg.Code {
	n, err := data.Delete()
	r := NewReturn()
	r.Check(err != nil, errMsg.DB_ERROR)
	r.Check(n < 1, errMsg.DB_OPTION_FAIL)
	return r.C
}
func UpdateCategory(cate *model.Category, data map[string]interface{}) errMsg.Code {
	n, err := cate.Update(data)
	r := NewReturn()
	r.Check(err != nil, errMsg.DB_ERROR)
	r.Check(n < 1, errMsg.DB_OPTION_FAIL)
	return r.C
}
func GetCateList(page *request.PageParam, m ...map[string]interface{}) (response.PageInfo, errMsg.Code) {
	var (
		count    int64
		err      error
		a        int64
		pageInfo = response.PageInfo{
			Total: &count,
			List:  nil,
		}
	)
	count, err = model.CountCateGory()
	r := NewReturn()
	r.Check(err != err, errMsg.DB_ERROR)
	r.IfDo(count > 0, func() {
		pageInfo.List, err, a = model.GetCateGoryList(page)
		fmt.Println(a)
	})
	r.Check(err != nil && err != gorm.ErrRecordNotFound, errMsg.DB_ERROR)
	return pageInfo, r.C
}

// 业务分析
/**
1.C不为SUCCESS，不继续进行后续判断,一路绿灯
2.C为SUCCESS，
*/
